package cn.agiledata.bank.common.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import cn.agiledata.bank.hibernateBean.AccountBank;
import cn.agiledata.bank.hibernateBean.BankInfo;
import cn.agiledata.bank.hibernateBean.CityInfo;
import cn.agiledata.bank.hibernateBean.VnvAccount;
import com.vnv.infrastructure.logic.IPBHibernate3BaseMgr;

public class BankTemplate extends IPBHibernate3BaseMgr {

    public CityInfo getCityInfo(String cityId) {
        return (CityInfo) getHibernateTemplate().find(
                "from CityInfo where cityId = ?", new Object[] { cityId }).get(
                0);
    }

    public BankInfo getBankInfo(String l) {
        if (StringUtils.isEmpty(l))
            return new BankInfo();
        return (BankInfo) getHibernateTemplate().get(BankInfo.class,
                new Long(l));
    }

    public void updateAccountBank(AccountBank ab) {
        getHibernateTemplate().saveOrUpdate(ab);
    }

    public void delAccountBank(AccountBank ab) {
        try {
            ab.setIsValid("N");

            getHibernateTemplate().saveOrUpdate(ab);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public AccountBank getAccountBankById(String l) {
        return (AccountBank) getHibernateTemplate().get(AccountBank.class,
                new Long(l));
    }

    public BankInfo getBankById(Long l) {
        return (BankInfo) getHibernateTemplate().get(BankInfo.class, l);
    }

    public VnvAccount getDefaultVnvAccountOfBank(String bankCode) {
        String hql = "from VnvAccount va where va.bankInfo.bankCode=?"
                + " and va.defaultFlag='Y' and va.status='Y' and va.accountType='N'";
        List resultList = getHibernateTemplate().find(hql, bankCode);
        if (resultList.size() == 0)
            return null;
        else
            return (VnvAccount) resultList.get(0);
    }
}
